//
//  ReadWebVC.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "ReadWebVC.h"
#import "CommentViewController.h"
#import "UMSocial.h"
#define  HEIGHT self.view.frame.size.height
#define  WIDTH self.view.frame.size.width
@interface ReadWebVC ()<NSCoding>
@property (nonatomic, copy)NSString *commentId;
@property (nonatomic, assign)BOOL isShare;
@end

@implementation ReadWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
 
    //数据处理
    [self dataHandle];
    //创建导航栏按钮
    }
- (void)creatButtonWithxinNum:(NSString *)xinnum comNum:(NSString *)comnum
{
    UIBarButtonItem *dianButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sangedian.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dianAction:)] autorelease];
    UIBarButtonItem *commentButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"commet.png"] style:UIBarButtonItemStylePlain target:self action:@selector(commentAction:)] autorelease];
    UIBarButtonItem *xinButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xin.png"] style:UIBarButtonItemStyleDone target:self action:@selector(xinAction:)] autorelease];
    [xinButton setTintColor:[UIColor blackColor]];
    [commentButton setTintColor:[UIColor blackColor]];
    //做个假标签
    UIView *comView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
  //  comView.backgroundColor = [UIColor redColor];
    UIBarButtonItem *comBtn = [[UIBarButtonItem alloc] initWithCustomView:comView];
    //做个假标签
    UIView *xinView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
   // xinView.backgroundColor = [UIColor redColor];
    UIBarButtonItem *xinBtn = [[UIBarButtonItem alloc] initWithCustomView:xinView];
    [dianButton setTintColor:[UIColor blackColor]];
    //创建人数Label
    UILabel *comLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    comLabel.text = comnum;
    [comView addSubview:comLabel];
    [comLabel release];
    UILabel *xinLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    xinLabel.text = xinnum;
    [xinView addSubview:xinLabel];
    [xinLabel release];

    self.navigationItem.rightBarButtonItems = @[dianButton, comBtn, commentButton, xinBtn,xinButton];
}
//分享与收藏触发按钮
- (void)dianAction:(UIBarButtonItem *)btn
{
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56a8766ce0f55af2530012db"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
                                       delegate:nil];
    
    
//    if (!_isShare) {
//    //灰屏
//    UIView *alView = [[UIView alloc] initWithFrame:self.view.bounds];
//    alView.backgroundColor = [UIColor blackColor];
//    alView.tag = 2000;
//    alView.alpha = 0.5;
//    [self.view addSubview:alView];
//    //分享界面
//    UIView *jumpView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 200, WIDTH, 200)];
//    jumpView.tag = 1500;
//    jumpView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:jumpView];
//    [jumpView release];
//    //收回Btn
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//   // backBtn.backgroundColor = [UIColor greenColor];
//    [backBtn setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
//    backBtn.frame = CGRectMake((WIDTH - 50) / 2, 0, 50, 30);
//    [jumpView addSubview:backBtn];
//    [backBtn addTarget:self action:@selector(backBtnAction)forControlEvents:UIControlEventTouchUpInside];
//    //分享到Label
//        UILabel *shareLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 60, 40)];
//        shareLabel.font = [UIFont boldSystemFontOfSize:20];
//        shareLabel.text = @"分享到";
//        [jumpView addSubview:shareLabel];
//        [shareLabel release];
//    //微信Btn
//        UIButton *weChatBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        weChatBtn.frame = CGRectMake(10, shareLabel.frame.size.height + 20, 60, 60);
//        weChatBtn.layer.cornerRadius = 30;
//        [weChatBtn setImage:[UIImage imageNamed:@"wechat.png"] forState:UIControlStateNormal];
//        [jumpView addSubview:weChatBtn];
//        UILabel *weChatLbel = [[UILabel alloc] initWithFrame:CGRectMake(weChatBtn.center.x - 20, weChatBtn.frame.origin.y + 60, 60, 20)];
//        weChatLbel.text = @"微信";
//        [jumpView addSubview:weChatLbel];
//        [weChatLbel release];
//    //朋友圈
//        UIButton *friendBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        friendBtn.frame = CGRectMake( weChatBtn.frame.size.width + 30, weChatBtn.frame.origin.y, 60, 60);
//        friendBtn.layer.cornerRadius = 30;
//        [friendBtn setImage:[UIImage imageNamed:@"pengyouquan.png"] forState:UIControlStateNormal];
//        [friendBtn setTintColor:[UIColor redColor]];
//        [jumpView addSubview:friendBtn];
//        UILabel *friendLbel = [[UILabel alloc] initWithFrame:CGRectMake(friendBtn.center.x - 20, weChatBtn.frame.origin.y + 60, 60, 20)];
//        friendLbel.text = @"朋友圈";
//        [jumpView addSubview:friendLbel];
//        [friendLbel release];
//
//    //微博
//        UIButton *weboBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        weboBtn.frame = CGRectMake( weChatBtn.frame.size.width * 2 + 60 + 40 + 15, weChatBtn.frame.origin.y, 60, 60);
//        weboBtn.layer.cornerRadius = 30;
//        [weboBtn setTintColor:[UIColor redColor]];
//        [weboBtn setImage:[UIImage imageNamed:@"weibo.png"] forState:UIControlStateNormal];
//        [jumpView addSubview:weboBtn];
//        UILabel *weboLbel = [[UILabel alloc] initWithFrame:CGRectMake(weboBtn.center.x - 20, weChatBtn.frame.origin.y + 60, 60, 20)];
//        weboLbel.text = @"微博";
//        [jumpView addSubview:weboLbel];
//        [weboLbel release];
//      //qq空间
//        UIButton *qqBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        qqBtn.frame = CGRectMake( weChatBtn.frame.size.width * 3 + 90 + 40, weChatBtn.frame.origin.y, 60, 60);
//        qqBtn.layer.cornerRadius = 30;
//        [qqBtn setImage:[UIImage imageNamed:@"qq.png"] forState:UIControlStateNormal];
//        [qqBtn setTintColor:[UIColor yellowColor]];
//        [jumpView addSubview:qqBtn];
//        UILabel *qqLbel = [[UILabel alloc] initWithFrame:CGRectMake(qqBtn.center.x - 20, weChatBtn.frame.origin.y + 60, 60, 20)];
//        qqLbel.text = @"qq空间";
//        [jumpView addSubview:qqLbel];
//        [qqLbel release];
//    //收藏
//        UIButton *scBtn = [UIButton buttonWithType:UIButtonTypeSystem];
//        scBtn.frame = CGRectMake((jumpView.frame.size.width - 60) / 2, weChatBtn.frame.origin.y , 60, 60);
//        scBtn.layer.cornerRadius = 30;
//        [scBtn setImage:[UIImage imageNamed:@"shoucang.png"] forState:UIControlStateNormal];
//        [scBtn setTintColor:[UIColor grayColor]];
//        [jumpView addSubview:scBtn];
//        UILabel *scLbel = [[UILabel alloc] initWithFrame:CGRectMake(scBtn.center.x - 20, weChatBtn.frame.origin.y + 60, 60, 20)];
//        scLbel.text = @"收藏";
//        [jumpView addSubview:scLbel];
//        [scLbel release];
//
//        
//    //添加动画
//    CAKeyframeAnimation *kf = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
//    kf.values = @[@(0),@(0.5),@(1.0)];
//    kf.duration = 0.5;
//    [jumpView.layer addAnimation:kf forKey:@"神马"];
//    }else{
//        [self backBtnAction];
//    }
//    _isShare = !_isShare;
}
- (void)backBtnAction
{
    [[self.view viewWithTag:1500] removeFromSuperview];
    [[self.view viewWithTag:2000] removeFromSuperview];
}
//跳转评论页
- (void)commentAction:(UIBarButtonItem *)btn
{
    CommentViewController *commentVC = [[CommentViewController alloc] init];
    commentVC.contenid = self.commentId;
    [self.navigationController pushViewController:commentVC animated:YES];
    [commentVC release];
}
#pragma mark - 本地收藏
- (void)xinAction:(UIBarButtonItem *)btn
{
    NSLog(@"%@",NSHomeDirectory());
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"collection.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];//解档
    if (arr) {
        for (Model *m in arr) {
            if ([m.content isEqualToString:_collectionModel.content]) {
                [self promptViewWithMessage:@"已收藏过!"];//提示已经收藏过
                return ;
            }
        }
        [arr addObject:self.collectionModel];
        [self promptViewWithMessage:@"收藏成功"];
    }else{
        arr = [NSMutableArray array];
        [arr addObject:self.collectionModel];
        [self promptViewWithMessage:@"收藏成功"];
    }
    [NSKeyedArchiver archiveRootObject:arr toFile:path];//归档
}

#pragma mark - 提示方法
- (void)promptViewWithMessage:(NSString *)message
{
    UIView *tishiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    tishiView.center = self.view.center;
    tishiView.backgroundColor = [UIColor blackColor];
    tishiView.alpha = 0.5;
    tishiView.layer.cornerRadius = 10;
    [self.view addSubview:tishiView];
    [tishiView release];
    UILabel *tishilabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 80, 30)];
    tishilabel.textColor = [UIColor whiteColor];
    tishilabel.text = message;
    [tishiView addSubview:tishilabel];
    [tishilabel release];
    //延时
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [tishiView removeFromSuperview];
    });
    
}

- (void)dataHandle
{
    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSDictionary *dic = @{@"contentid":self.contenId,@"client":@2};
    [PWNetWorking POSTWithURL:urlStr body:dic completion:^(id result) {
        NSString *html = [result[@"data"] objectForKey:@"html"];
        NSString *newHtml = [NSString stringWithFormat:@"<head><style>img{max-width:320px !important;}</style></head>%@",html];
        UIWebView *wb = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [wb loadHTMLString:newHtml baseURL:nil];
        [self.view addSubview:wb];
        [wb release];
        NSString *comment = [result[@"data"][@"counterList"][@"comment"] stringValue];
        NSString *like = [result[@"data"][@"counterList"][@"like"] stringValue];
        [self creatButtonWithxinNum:like comNum:comment];
        self.commentId = result[@"data"][@"contentid"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
